/*
 * global.cc
 *
 *  Created on: Dec 16, 2014
 *      Author: root
 */
/******************************************************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "global.h"
/******************************************************************************************************************/
TCP_VEC g_tcp_v;
UDP_VEC g_udp_v;
TCP_MAP g_tcp_m;
UDP_MAP g_udp_m;
int g_ip_count = 0;
char g_label[64] = { 0 };
bool g_flag = false;
/******************************************************************************************************************/
double ConvertTimevalToDouble(struct timeval time) {
	return time.tv_sec + time.tv_usec / MICRO_SEC;
}
bool GlobalInit() {
//	nids_params.filename = (char*) PCAP_FILE_UDP;
//	nids_params.device = "eth1";
	g_ip_count = 0;
//	UDP_VEC
	g_udp_v.reserve(256);
	g_tcp_v.reserve(256);
	g_flag = false;
	return true;
}
void GlobalEnd() {
	TCP_MAP::iterator it;
	for (it = g_tcp_m.begin(); it != g_tcp_m.end(); it++) {
		it->first.ToString();
	}
}

TcpStat* FindTcpStream(Socket client, Socket server) {
	Stream stream(client, server);
	TCP_MAP::iterator it = g_tcp_m.find(stream);
	if (it != g_tcp_m.end()) {	// find it
		return it->second;
	}
	// do not find.
	return NULL;
}
UdpStat* FindUdpStream(Socket client, Socket server) {
	Stream stream(client, server);
	UDP_MAP::iterator it = g_udp_m.find(stream);
	if (it != g_udp_m.end()) {
		return it->second;
	}
	return NULL;
}
int IsClientToServer(Socket src, Socket dest) {
	Stream stream(src, dest);
	TCP_MAP::iterator it = g_tcp_m.find(stream);
	if (it != g_tcp_m.end()) {	// find it
			// from c to s
		return CLIENT_TO_SERVER;
	}
	Stream tmp(dest, src);
	it = g_tcp_m.find(tmp);
	if (it != g_tcp_m.end()) {	// find it
			// from s to c
		return SERVER_TO_CLIENT;
	}
	return DO_NOT_EXIST;
}
bool FreeTcpStream(Socket client, Socket server) {
	Stream stream(client, server);
	TCP_MAP::iterator it = g_tcp_m.find(stream);
	if (it != g_tcp_m.end()) {	// find it exists
//		printf("NO.%d ", g_tcp.size());
//		it->first.ToString();
//		it->second->ToString();
		delete it->second;	// free tcp stream related space.
		g_tcp_m.erase(it);
		return true;
	}	// do not find.
	return false;
}

int GetTcpClientState(Socket client, Socket server) {
	// we assume the tcp stream exists
	return FindTcpStream(client, server)->c_state;

}
int GetTcpServerState(Socket client, Socket server) {
	// we assume the tcp stream exists
	return FindTcpStream(client, server)->s_state;

}

void AddToTcpVector(TcpStat* stat) {
	TcpRecord* r = new TcpRecord(stat);
	g_tcp_v.push_back(r);
	printf("%d\n", g_tcp_v.size());
	r->ToString();
}
void AddToUdpVector(UdpStat* stat) {
	UdpRecord* r = new UdpRecord(stat);
	g_udp_v.push_back(r);
	printf("%d\n", g_udp_v.size());
	r->ToString();
}
void SleepCertainTime() {
	srand((int) time(NULL));
	usleep(rand() % PKT_INTERVAL + 100);
}
/******************************************************************************************************************/
